package dbgroup02.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import dbgroup02.bean.Baggage;
import dbgroup02.bean.BaggageWithTicket;
import dbgroup02.bean.BoardingPass;
import dbgroup02.bean.CheckInInfo;
import dbgroup02.bean.FlightTicket;
import dbgroup02.bean.Passenger;
import dbgroup02.bean.Ticket;
import dbgroup02.dao.BaggageDao;
import dbgroup02.dao.BoardingPassDao;
import dbgroup02.dao.CheckInInfoDao;
import dbgroup02.dao.FlightTicketDao;
import dbgroup02.dao.PassengerDao;
import dbgroup02.dao.TicketDao;

public class BaggageCheckInController extends SimpleFormController {
	
	private PassengerDao passengerDao;
	private TicketDao ticketDao;
	private BaggageDao baggageDao;
	private FlightTicketDao flightTicketDao;
	private CheckInInfoDao checkInInfoDao;
	private BoardingPassDao boardingPassDao;
	
	public void setBoardingPassDao(BoardingPassDao boardingPassDao) {
		this.boardingPassDao = boardingPassDao;
	}

	public void setPassengerDao(PassengerDao passengerDao) {
		this.passengerDao = passengerDao;
	}
	
	public void setTicketDao(TicketDao ticketDao) {
		this.ticketDao = ticketDao;
	}

	public void setBaggageDao(BaggageDao baggageDao) {
		this.baggageDao = baggageDao;
	}
	
	public void setFlightTicketDao(FlightTicketDao flightTicketDao) {
		this.flightTicketDao = flightTicketDao;
	}
	
	public void setCheckInInfoDao(CheckInInfoDao checkInInfoDao) {
		this.checkInInfoDao = checkInInfoDao;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
		
		BaggageWithTicket baggageWithTicket = (BaggageWithTicket) command;
		
		int idTicket = baggageWithTicket.getIdTicket();
		
		Ticket ticket = ticketDao.getTicket(idTicket);
		
		List<FlightTicket> flightTickets = flightTicketDao.getFlightTickets(idTicket);
		
		CheckInInfo checkInInfo = checkInInfoDao.getCheckInfoByFlight(flightTickets.get(0).getIdFlight());
		
		Baggage baggage = new Baggage();
		
		baggage.setType(baggageWithTicket.getType());
		baggage.setIdCheckInInfo(checkInInfo.getIdCheckInInfo());
		baggage.setWeight(baggageWithTicket.getWeight());
		
		baggageDao.addBaggage(baggage);
		
		return new ModelAndView("redirect:boardingpasses.app?idTicket=" + ticket.getIdTicket());
	}


	@Override
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (request.getMethod().equals("GET")) {
			int idTicket = -1;
			
			idTicket = Integer.parseInt(request.getParameter("idTicket"));
			Ticket ticket = ticketDao.getTicket(idTicket);
			
			int idPassenger = ticket.getIdPassenger();
			Passenger passenger = passengerDao.getPassenger(idPassenger);
			
			List<FlightTicket> flightTickets = flightTicketDao.getFlightTickets(idTicket);
					
			BaggageWithTicket baggageWithTicket = new BaggageWithTicket();
			baggageWithTicket.setIdTicket(idTicket);
			
			
			
			List<BoardingPass> boardingPasses = new ArrayList<BoardingPass>();
			for (FlightTicket flightTicket: flightTickets) {
				boardingPasses.add(boardingPassDao.getBoardingPass(flightTicket.getIdBoardingPass()));
			} 
			
			
			
			
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("passenger", passenger);
			model.put("flightTickets", flightTickets);
			model.put("ticket", ticket);
			model.put("command", baggageWithTicket);
			model.put("boardingPasses", boardingPasses);
			return new ModelAndView(getFormView(), model);			
		}
		else
			return super.handleRequest(request, response);
	}
}
